import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class SystemEssentials extends JPanel implements ActionListener {
	
	private int pageID;
	
	public JLabel lHeadertext, lFilter, lViewColumn;
	public JButton bEdit, bCancel, bBack, bSave, bSearch, bFilter;
	public JTextField tfSearch;
	
	public JTable table;
	public DefaultTableModel model;
	public JScrollPane scroll;
	public DefaultTableCellRenderer cellRenderer;
	
	private ImageIcon iFilter = new ImageIcon("gui/labels/labelFilter.png"),
	  				  iView = new ImageIcon("gui/labels/labelView.png"),
	  				  iBack = new ImageIcon("gui/buttons/bBack.png"),
	  				  iBack_ = new ImageIcon("gui/buttons/bBack_.png"),
	  				  iCancel = new ImageIcon("gui/buttons/bCancel.png"),
	  				  iCancel_ = new ImageIcon("gui/buttons/bCancel_.png"),
	  				  iEdit = new ImageIcon("gui/buttons/bEdit.png"),
	  				  iEdit_ = new ImageIcon("gui/buttons/bEdit_.png"),
	  				  iSave = new ImageIcon("gui/buttons/bSave.png"),
	  				  iSave_ = new ImageIcon("gui/buttons/bSave_.png");
	  				   
	
	public SystemEssentials (int g){
		setLayout(null);
		setBounds(0,0,700,600);	
		setOpaque(false);
		
		if(g >= 1 && g <= 15)
			createGUI(g);
		
		pageID = g;
	}
	
	public int getPageID(){
		return pageID;
	}
	
	public void createGUI(int g){
		// Header
		switch(g){
		case 1: lHeadertext = new JLabel(new ImageIcon("gui/labels/labelEmp.png")); break;
		case 2: 
		case 3: lHeadertext = new JLabel(new ImageIcon("gui/labels/labelSec.png")); break;
		case 4: 
		case 5: 
		case 6:
		case 7:
		case 8:
		case 9: lHeadertext = new JLabel(new ImageIcon("gui/labels/labelGrades.png")); break;
		case 10:
		case 11:
		case 12: lHeadertext = new JLabel(new ImageIcon("gui/labels/labelComp.png")); break;
		case 13: lHeadertext = new JLabel(new ImageIcon("gui/labels/labelGrades.png")); break;
		}
		lHeadertext.setBounds(500,35,184,61);
		add(lHeadertext);
		
		// Table
		table = new JTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		table.getTableHeader().setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		table.getTableHeader().setReorderingAllowed(false);
		table.setColumnSelectionAllowed(false);
		table.setRowSelectionAllowed(true);
		table.setRowHeight(22);
		table.setEnabled(false);
		table.setToolTipText("Implementation not fully done.");
		scroll = new JScrollPane(table);
		scroll.setBounds(15,130,665,280);
		add(scroll);
		resetTableModel(g);
		
		//System Buttons
		bBack = new JButton(iBack);
		bBack.setBounds(440,510,123,53);
		bBack.setRolloverIcon(iBack_);
		bBack.setBorderPainted(false);
		bBack.setContentAreaFilled(false);
		bBack.setVisible(true);
		bBack.addActionListener(this);
		add(bBack);

		bCancel = new JButton(iCancel);
		bCancel.setBounds(560,510,123,53);
		bCancel.setRolloverIcon(iCancel_);
		bCancel.setBorderPainted(false);
		bCancel.setContentAreaFilled(false);
		bCancel.setVisible(true);
		bCancel.setEnabled(false);
		bCancel.addActionListener(this);
		add(bCancel);
		
		bEdit = new JButton(iEdit);
		bEdit.setBounds(440,430,123,53);
		bEdit.setRolloverIcon(iEdit_);
		bEdit.setBorderPainted(false);
		bEdit.setContentAreaFilled(false);
		bEdit.setVisible(true);
		bEdit.addActionListener(this);
		add(bEdit);
		
		bSave = new JButton(iSave);
		bSave.setBounds(560,430,123,53);
		bSave.setRolloverIcon(iSave_);
		bSave.setBorderPainted(false);
		bSave.setContentAreaFilled(false);
		bSave.setVisible(true);
		bSave.setEnabled(false);
		bSave.setToolTipText("This will produce all the prompts available for data entry.");
		bSave.addActionListener(this);
		add(bSave);
		
		// Filter
		lViewColumn = new JLabel(iView);
		lViewColumn.setBounds(20,415,125,19);
		add(lViewColumn);
		
		lFilter = new JLabel(iFilter);
		lFilter.setBounds(170,415,138,19);
		add(lFilter);
		
		bFilter = new JButton("Filter");
		bFilter.setBounds(365,480,70,30);
		add(bFilter);
			
		//Search
		tfSearch = new JTextField();
		tfSearch.setBounds(170,545,190,30);
		add(tfSearch);
		
		bSearch = new JButton("Search");
		bSearch.setBounds(365,545,70,30);
		add(bSearch);
		
		initGUI();
	}
	
	public void initGUI(){
		
	}
	
	public void actionPerformed(ActionEvent a) {
		Object[] options = {"Yes","No"};
		int choice = 1;
		
		if(a.getSource() == bEdit){
			table.setEnabled(true);
			bSave.setEnabled(true);
			bCancel.setEnabled(true);
		}
		if(a.getSource() == bSave){
			JOptionPane.showConfirmDialog(null, "Invalid data in some fields. Please enter again.", "LSQC Grading System Software", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
			JOptionPane.showConfirmDialog(null, "Please complete all fields.", "LSQC Grading System Software", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
			
			switch(pageID){
			case 1:
				choice = JOptionPane.showOptionDialog(null,"Done editing employee details?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 2:
				choice = JOptionPane.showOptionDialog(null,"Done editing section details?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 3:
				choice = JOptionPane.showOptionDialog(null,"Done editing class list?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 4:
				JOptionPane.showConfirmDialog(null, "Active components exceed 100%.", "LSQC Grading System Software", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
				choice = JOptionPane.showOptionDialog(null,"Done editing grade components?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 5:
				choice = JOptionPane.showOptionDialog(null,"Done editing subject grades?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 6:
				choice = JOptionPane.showOptionDialog(null,"Done editing conduct grades?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 7:
				choice = JOptionPane.showOptionDialog(null,"Done editing co-curricular grades?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 8:
				choice = JOptionPane.showOptionDialog(null,"Done editing rhgp grades?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 10:
				choice = JOptionPane.showOptionDialog(null,"Done editing conduct components?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 11:
				choice = JOptionPane.showOptionDialog(null,"Done editing co-curricular components?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
				break;
			case 12:
				choice = JOptionPane.showOptionDialog(null,"Done editing rhgp components?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
			case 13:
				choice = JOptionPane.showOptionDialog(null,"Done editing attendance?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
			}
			
			if(choice == 0) {
				JOptionPane.showConfirmDialog(null, "Saved succesfully.", "LSQC Grading System Software", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
			}
		}
		
		if(a.getSource() == bBack){
			MainPanel.goToPage(1);
		}

		if(a.getSource() == bCancel){
			choice = JOptionPane.showOptionDialog(null,"Information entered will not be saved. Continue?","LSQC Grading System Software",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
			if(choice == 1){
				bSave.setEnabled(false);
				bCancel.setEnabled(false);
			}
		}
	}

	// Show/hide column
	public void setColumnWidth(int c, int x){
		int w = 0;
		switch(this.getPageID()){
		case 1:
			switch(c){
			case 0: w = 50; break;
			case 1: w = 70; break;
			case 2: w = 100; break;
			case 3: w = 100; break;
			case 4: w = 130; break;
			case 5: w = 150; break;
			case 6: w = 140; break;
			}
			break;
		case 2:
			switch(c){
			case 0: w = 50; break;
			case 1: w = 70; break;
			case 2: w = 150; break;
			case 3: w = 200; break;
			case 4: w = 200; break;
			}
			break;
		case 3:
			switch(c){	
			case 0: w = 80; break;
			case 1: w = 70; break;
			case 2: w = 250; break;
			case 3: w = 250; break;
			}
			break;
		case 4:
			switch(c){	
			case 0: w = 70; break;
			case 1: w = 80; break;
			case 2: w = 200; break;
			case 3: w = 200; break;
			case 4: w = 200; break;
			}
			break;
		case 5:
			switch(c){
			case 0: w = 80; break;
			case 1: w = 50; break;
			case 2: w = 80; break;
			case 3: w = 80; break;
			case 4: w = 50; break;
			case 5: w = 50; break;
			case 6: w = 50; break;
			case 7: w = 50; break;
			case 8: w = 50; break;
			case 9: w = 50; break;
			case 10: w = 50; break;
			case 11: w = 50; break;
			case 12: w = 50; break;
			case 13: w = 50; break;
			case 14: w = 50; break;
			case 15: w = 50; break;
			case 16: w = 50; break;
			case 17: w = 50; break;
			case 18: w = 50; break;
			case 19: w = 50; break;
			case 20: w = 50; break;
			case 21: w = 50; break;
			case 22: w = 50; break;
			case 23: w = 50; break;
			case 24: w = 50; break;
			case 25: w = 50; break;
			case 26: w = 50; break;
			case 27: w = 50; break;
			case 28: w = 50; break;
			case 29: w = 50; break;
			case 30: w = 50; break;
			case 31: w = 50; break;
			case 32: w = 50; break;
			case 33: w = 50; break;
			case 34: w = 50; break;
			case 35: w = 50; break;
			case 36: w = 50; break;
			}
			break;
		case 6:
			switch(c){
			case 0: w = 80; break;
			case 1: w = 50; break;
			case 2: w = 80; break;
			case 3: w = 80; break;
			case 4: w = 100; break;
			case 5: w = 100; break;
			case 6: w = 100; break;
			case 7: w = 100; break;
			case 8: w = 100; break;
			case 9: w = 100; break;
			case 10: w = 100; break;
			}
			break;
		case 7:
			switch(c){
			case 0: w = 80; break;
			case 1: w = 50; break;
			case 2: w = 80; break;
			case 3: w = 80; break;
			case 4: w = 100; break;
			case 5: w = 100; break;
			}
			break;
		case 8:
			switch(c){
			case 0: w = 80; break;
			case 1: w = 50; break;
			case 2: w = 80; break;
			case 3: w = 80; break;
			case 4: w = 100; break;
			case 5: w = 100; break;
			case 6: w = 100; break;
			}
			break;
		case 10:
			switch(c){	
			case 0: w = 80; break;
			case 1: w = 70; break;
			case 2: w = 250; break;
			case 3: w = 250; break;
			}
			break;
		case 11:
			switch(c){	
			case 0: w = 70; break;
			case 1: w = 80; break;
			case 2: w = 210; break;
			case 3: w = 210; break;
			}
			break;
		case 12:
			switch(c){	
			case 0: w = 70; break;
			case 1: w = 80; break;
			case 2: w = 210; break;
			case 3: w = 210; break;
			}
			break;
		case 13:
			switch(c){	
			case 0: w = 70; break;
			case 1: w = 80; break;
			case 2: w = 150; break;
			case 3: w = 150; break;
			case 4: w = 150; break;
			case 5: w = 150; break;
			}
			break;
		}
		try {
			table.getColumnModel().getColumn(c).setMinWidth(w*x);
			table.getColumnModel().getColumn(c).setMaxWidth(w*x);
			table.getTableHeader().getColumnModel().getColumn(c).setMinWidth(w*x);
			table.getTableHeader().getColumnModel().getColumn(c).setMaxWidth(w*x);
		} catch (ArrayIndexOutOfBoundsException e) {
			// do nothing
		}
	}
	
	// Reset Table Model
	public void resetTableModel(int g){
		model = new DefaultTableModel();
		model.addColumn("ID");
		model.addColumn("Status");
		switch(g){
		case 1: 
			model.addColumn("First Name");
			model.addColumn("Last Name");
			model.addColumn("Position");
			model.addColumn("Area");
			model.addColumn("Year Handled");
			break;
		case 2: 
			model.addColumn("Year Level");
			model.addColumn("Section Name");
			model.addColumn("Adviser");
			break;
		case 3:
			model.addColumn("First Name");
			model.addColumn("Last Name");
			break;
		case 4:
			model.addColumn("Subject Component");
			model.addColumn("Percentage");
			model.addColumn("No. of Subcomponents");
			break;
		case 5:
			model.addColumn("First Name");
			model.addColumn("Last Name");
			model.addColumn("Quiz 1");
			model.addColumn("Quiz 2");
			model.addColumn("Quiz 3");
			model.addColumn("Quiz 4");
			model.addColumn("Quiz 5");
			model.addColumn("Quiz 6");
			model.addColumn("Incentives");
			model.addColumn("Quiz Total");
			model.addColumn("Percentage Equivalent");
			model.addColumn("Seatwork 1");
			model.addColumn("Seatwork 2");
			model.addColumn("Seatwork 3");
			model.addColumn("Seatwork 4");
			model.addColumn("Seatwork 5");
			model.addColumn("Seatwork Total");
			model.addColumn("Percentage Equivalent");
			model.addColumn("Homework 1");
			model.addColumn("Homework 2");
			model.addColumn("Homework 3");
			model.addColumn("Homework 4");
			model.addColumn("Homework 5");
			model.addColumn("Homework Total");
			model.addColumn("Percentage Equivalent");
			model.addColumn("Project");
			model.addColumn("Percentage Equivalent");
			model.addColumn("Long Test");
			model.addColumn("Percentage Equivalent");
			model.addColumn("Class Standing");
			model.addColumn("Final Exam");
			model.addColumn("Percentage Equivalent");
			model.addColumn("Final Grade");
			break;
		case 6:
			model.addColumn("First Name");
			model.addColumn("Last Name");
			model.addColumn("Love for God");
			model.addColumn("Love for Country");
			model.addColumn("Love for Work");
			model.addColumn("Love for Self");
			model.addColumn("Love for Others");
			model.addColumn("Total");
			model.addColumn("Equivalent");
			break;
		case 7:
			model.addColumn("First Name");
			model.addColumn("Last Name");
			model.addColumn("Participation");
			model.addColumn("Equivalent");
			break;
		case 8:
			model.addColumn("First Name");
			model.addColumn("Last Name");
			model.addColumn("Punctuality");
			model.addColumn("Activeness");
			model.addColumn("Total");
			break;
		case 10:
			model.addColumn("Conduct Component");
			model.addColumn("Percentage");
			break;
		case 11:
			model.addColumn("Co-Curricular Component");
			model.addColumn("Percentage");
			break;
		case 12:
			model.addColumn("RHGP Component");
			model.addColumn("Percentage");
			break;
		case 13:
			model.addColumn("First Name");
			model.addColumn("Last Name");
			model.addColumn("Days Tardy");
			model.addColumn("Days Absent");
			break;
		}
		table.setModel(model);
	}
			
	// GUI Full Refresh
	public void refreshGUI(){
		tfSearch.setText("");		
	}
}